/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @author Simon Oliver
 * @version 1.0
 */

import wilberforce.geom.*;
import wilberforce.simulation.particles.*;
import wilberforce.ui.AbstractSlider;
import wilberforce.util.movieclip.MovieClipUtil;
import wilberforce.util.textField.textFieldUtility;

import com.bourre.events.BasicEvent;
import com.bourre.commands.Delegate;

class wilberforce_demos.particles.particleSystemDemo1 extends MovieClip 
{
	static var _emitterBox:rect=new rect (-5,-5,5,5);
	private var _emitter:particleEmitter2D;
	private var _system:particleSystem2D;
	
	
	private var _particleSystemContainer:MovieClip;
	
	private var _gravitySlider:AbstractSlider;
	private var _velocitySlider:AbstractSlider;
	private var _velocitySliderMax:AbstractSlider;
	private var _numParticlesSlider:AbstractSlider;
	private var _lifetimeSlider:AbstractSlider;
	private var _lifetimeSliderMax:AbstractSlider;
	private var _emitRateSlider:AbstractSlider;
	private var _directionSlider:AbstractSlider;
	private var _spreadSlider:AbstractSlider;
	
	public var resetButton:MovieClip;
	
	
	private var _labelTextFormat:TextFormat;
	
	private var _gravityForce:vector2D;
	
	function particleSystemDemo1(container)
	{
		container.__proto__ = this.__proto__;
		container.__constructor__ = particleSystemDemo1;
		this = container;	
		_init();
	}
	private function _init(Void):Void
	{
		
		var _numParticles:Number=50;
		var _particleMinLifetime:Number=25;
		var _particleMaxLifetime:Number=50;
		var _direction=-Math.PI/2
		var _spread=Math.PI
		var _emitRate=2;
			
		_labelTextFormat=new TextFormat("_sans",10,0xEEEEEE,false,false);
		_particleSystemContainer=this.createEmptyMovieClip("system",this.getNextHighestDepth());
		_particleSystemContainer._x=Stage.width/2-50;
		_particleSystemContainer._y=Stage.height/2;
		_emitter=new particleEmitter2D(_emitterBox,_emitRate,_direction,_spread,3,3,_particleMinLifetime,_particleMaxLifetime,1,1);
		//_system=new particleSystem2D(_particleSystemContainer,"smokeParticle",_emitter,_numParticles);
		_system=new particleSystem2D(_particleSystemContainer,"circleParticle",_emitter,_numParticles);
		
		_gravityForce=new vector2D(0,0.1);
		_system.addConstantForce(_gravityForce);
		
		_gravitySlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,0,0.5,_gravityForce.y,false,true,_labelTextFormat,"Gravity",_labelTextFormat);
		_gravitySlider.container._x=430;
		_gravitySlider.container._y=40;
		_gravitySlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setGravity)
		
		_velocitySlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,0,10,1,false,true,_labelTextFormat,"Velocity min",_labelTextFormat);
		_velocitySlider.container._x=430;
		_velocitySlider.container._y=80;
		_velocitySlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setVelocityMin)
		
		_velocitySliderMax=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,0,10,1,false,true,_labelTextFormat,"Velocity max",_labelTextFormat);
		_velocitySliderMax.container._x=430;
		_velocitySliderMax.container._y=120;
		_velocitySliderMax.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setVelocityMax)
	
		_numParticlesSlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,1,100,_numParticles,false,true,_labelTextFormat,"No. of particles",_labelTextFormat);
		_numParticlesSlider.container._x=430;
		_numParticlesSlider.container._y=160;
		_numParticlesSlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setNumParticles)
		
		_lifetimeSlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,1,100,_particleMinLifetime,false,true,_labelTextFormat,"Lifetime min.",_labelTextFormat);
		_lifetimeSlider.container._x=430;
		_lifetimeSlider.container._y=200;
		_lifetimeSlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setMinLifetime)
		
		_lifetimeSliderMax=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,1,100,_particleMaxLifetime,false,true,_labelTextFormat,"Lifetime max.",_labelTextFormat);
		_lifetimeSliderMax.container._x=430;
		_lifetimeSliderMax.container._y=240;
		_lifetimeSliderMax.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setMaxLifetime)
		
		_emitRateSlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,0.1,100,_emitRate,false,true,_labelTextFormat,"Emit rate",_labelTextFormat);
		_emitRateSlider.container._x=430;
		_emitRateSlider.container._y=280;
		_emitRateSlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setEmitRate)
		
		_directionSlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,-Math.PI*2,Math.PI*2,_direction,false,true,_labelTextFormat,"Direction Angle",_labelTextFormat);
		_directionSlider.container._x=430;
		_directionSlider.container._y=320;
		_directionSlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setDirection)
		
		_spreadSlider=new AbstractSlider(MovieClipUtil.createEmptyClip(this),100,0,Math.PI*2,_spread,false,true,_labelTextFormat,"Spread",_labelTextFormat);
		_spreadSlider.container._x=430;
		_spreadSlider.container._y=360;
		_spreadSlider.addEventListener(AbstractSlider.SLIDER_VALUE_CHANGED_EVENT,this,setSpread)
		
		resetButton.onPress=Delegate.create(this,resetSystem);
	}
	
	function resetSystem()
	{
		_system.reset();
	}
	
	private function setGravity(e:BasicEvent) {_gravityForce.y=e.getTarget().value;}
	
	private function setVelocityMin(e:BasicEvent) {_emitter._minVelocity=e.getTarget().value;}
	
	private function setVelocityMax(e:BasicEvent) {_emitter._maxVelocity=e.getTarget().value;}
	
	private function setNumParticles(e:BasicEvent) {_system.maxParticles=e.getTarget().value;}
	
	private function setMinLifetime(e:BasicEvent) {_emitter._minLifeTime=e.getTarget().value;}
	
	private function setMaxLifetime(e:BasicEvent) {_emitter._maxLifeTime=e.getTarget().value;}
	
	private function setEmitRate(e:BasicEvent) {_emitter._emitRate=e.getTarget().value;}
	
	private function setDirection(e:BasicEvent) {_emitter.directionAngle=e.getTarget().value;}
	
	private function setSpread(e:BasicEvent) {_emitter.rangeAngle=e.getTarget().value;}
	
}